#pragma once
#include <vector>
#include <boost/asio.hpp>
#include "Singleton.h"

using namespace std;
using namespace boost::asio;

class IOServicePool : public Singleton<IOServicePool>
{
    friend class Singleton<IOServicePool>;

private:
    explicit IOServicePool(size_t size = thread::hardware_concurrency());

public:
    ~IOServicePool();

    IOServicePool(const IOServicePool&) = delete;

    IOServicePool& operator=(const IOServicePool&) = delete;

    // 使用 round-robin 的方式返回一个 io_service
    io_context& GetIOService();

    void Stop();

private:
    vector<io_context> _io_contexts;
    vector<unique_ptr<io_context::work>> _works;
    vector<thread> _threads;
    size_t _next_io_context;
};
